#ifndef SEMPHORE_H
#define SEMPHORE_H

#include <memory>
#include <mutex>
#include <condition_variable>
class Semphore
{
private:
    int resLimit_;
    std::mutex mtx_;
    std::condition_variable cond_;

public:
    Semphore(int limit = 0) : resLimit_(limit)
    {
    }
    ~Semphore() = default;
    // 获取一个信号量资源
    void wait()
    {
        std::unique_lock<std::mutex> lock(mtx_);
        cond_.wait(lock, [&]() -> bool
                   { return resLimit_ > 0; });
        resLimit_--;
    }
    // 增加一个信号量资源
    void post()
    {  
            std::unique_lock<std::mutex> lock(mtx_);
            resLimit_++;
            cond_.notify_all();
    }
};

#endif // SEMPHORE_H ///:~